package com.abc.pay.client.ebus;

import com.abc.pay.client.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class PaymentResult extends TrxResponse {
    public PaymentResult(String aMessage) throws TrxException {
        super("Notify", aMessage);
        SignService sign;
        boolean result = false;
        try {
            result = true;
            log.info("TrustPayClient Java V3.2.2 交易开始==========================");
            log.info("接收到的支付结果通知：\n[" + aMessage + "]");
            String tMessage = Base64Code.Decode64(aMessage);
            log.info("经过Base64解码后的支付结果通知：\n[" + tMessage + "]");
            log.info("验证支付结果通知的签名：");
            sign = SignService.getUniqueInstance();
            XMLDocument tResult = sign.verifySignXML(new XMLDocument(tMessage));
            log.info("验证通过！\n 经过验证的支付结果通知：\n[" + tResult.toString() + "]");
            this.init(tResult);
            result = true;
        } catch (TrxException e) {
            this.setReturnCode(e.getCode());
            this.setErrorMessage(e.getMessage() + "-" + e.getDetailMessage());
            log.error("验证失败！\n");
            result = false;
        } finally {
            if (result) {
                log.info("交易成功结束==================================================");
            }else {
                log.info("交易失败结束==================================================");
            }
        }
    }
}
